home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / New System Software Extensions / OpenDoc A6 / OpenDoc Parts Framework / OPF / Examples / Draw / Sources / DrawFacet.cpp next >
Encoding:
Text File  |  1994-04-21  |  13.3 KB  |  513 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                DrawFacet.cpp
  4. //    Release Version:    $ 1.0d1 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //    Creation Date:        3/28/94
  8. //
  9. //    Copyright:    © 1993, 1994 by Apple Computer, Inc., all rights reserved.
  10. //
  11. //========================================================================================
  12.  
  13. #ifndef DRAWFACET_H
  14. #include "DrawFacet.h"
  15. #endif
  16.  
  17. #ifndef DRAWPART_H
  18. #include "DrawPart.h"
  19. #endif
  20.  
  21. #ifndef DRAWSELECTION_H
  22. #include "DrawSelection.h"
  23. #endif
  24.  
  25. #ifndef SHAPES_H
  26. #include "Shapes.h"
  27. #endif
  28.  
  29. #ifndef DRAWFRAME_H
  30. #include "DrawFrame.h"
  31. #endif
  32.  
  33. // ----- Framework Includes -----
  34.  
  35. #ifndef FWUTIL_H
  36. #include "FWUtil.h"
  37. #endif
  38.  
  39. #ifndef FWFRMING_H
  40. #include "FWFrming.h"
  41. #endif
  42.  
  43. // ----- OPF Foundation Includes -----
  44.  
  45. #ifndef BCOLL_H
  46. #include <BCColl.h>
  47. #endif
  48.  
  49.  
  50. // ----- Graphix Includes -----
  51.  
  52. #ifndef FWGCONST_H
  53. #include "FWGConst.h"
  54. #endif
  55.  
  56. #ifndef FWSTYLE_H
  57. #include "FWStyle.h"
  58. #endif
  59.  
  60. #ifndef FWDFAULT_H
  61. #include "FWDFault.h"
  62. #endif
  63.  
  64. #ifndef FWTXTSHP_H
  65. #include "FWTxtShp.h"
  66. #endif
  67.  
  68. // ----- OpenDoc Includes -----
  69.  
  70. #ifndef _TRNSFORM_
  71. #include <Trnsform.h>
  72. #endif
  73.  
  74. #ifndef _XMPSESSN_
  75. #include <XMPSessM.h>
  76. #endif
  77.  
  78. #ifndef _DISPTCH_
  79. #include <Disptch.h>
  80. #endif
  81.  
  82. // ----- Macintosh Includes -----
  83.  
  84. #if defined(FW_BUILD_MAC) && !defined(__EVENTS__)
  85. #include <Events.h>        // GetMouse, StillDown.
  86. #endif
  87.  
  88. #if defined(FW_BUILD_MAC) && !defined(__QUICKDRAW__)
  89. #include <Quickdraw.h>
  90. #endif
  91.  
  92. #if defined(FW_BUILD_MAC) && !defined(__TOOLUTILS__)
  93. #include <ToolUtils.h>
  94. #endif
  95.  
  96. #if defined(FW_BUILD_MAC) && !defined(__LIMITS__)
  97. #include <limits.h>
  98. #endif
  99.  
  100. #if defined(FW_BUILD_MAC) && !defined(__STDLIB__)
  101. #include <StdLib.h>
  102. #endif
  103.  
  104. #if defined(FW_BUILD_MAC) && !defined(__DRAG__)
  105. #include <Drag.h>
  106. #endif
  107.  
  108. #if defined(FW_BUILD_MAC) && !defined(mathRoutinesIncludes)
  109. #include <math routines.h>
  110. #endif
  111.  
  112. #pragma segment drawpart
  113.  
  114. //=========================================================================
  115. // CDrawFacet
  116. //=========================================================================
  117.  
  118. //------------------------------------------------------------------------------
  119. // CDrawFacet::CDrawFacet
  120. //------------------------------------------------------------------------------
  121.  
  122. CDrawFacet::CDrawFacet()
  123. {
  124. }
  125.  
  126. //------------------------------------------------------------------------------
  127. // CDrawFacet::IDrawFacet
  128. //------------------------------------------------------------------------------
  129.  
  130. void CDrawFacet::IDrawFacet(XMPFacet* xmpFacet, CDrawPart* drawPart)
  131. {
  132.     InitFacet(xmpFacet);
  133.     fDrawPart = drawPart;
  134. }
  135.  
  136. //------------------------------------------------------------------------------
  137. // CDrawFacet::~CDrawFacet
  138. //------------------------------------------------------------------------------
  139.  
  140. CDrawFacet::~CDrawFacet()
  141. {
  142. }
  143.  
  144. //------------------------------------------------------------------------------
  145. // CDrawFacet::Draw
  146. //------------------------------------------------------------------------------
  147.  
  148. void CDrawFacet::Draw(FW_CGraphicContext* gc)
  149. {
  150.     XMPShape* clipShape = ::NewXMPShape();
  151.     gc->GetClipShape(clipShape);
  152.     XMPShape* svClipShape = ::NewXMPShape(clipShape);
  153.     
  154.     ::EraseRgn(clipShape->GetQDRegion());
  155.  
  156.     BC_TCollectionActiveIterator<CBaseShape*> ite(*fDrawPart->GetShapeList());
  157.     while (!ite.IsDone())
  158.     {
  159.         (*ite.CurrentItem())->DrawShape(gc);
  160.         ite.Next();
  161.     }
  162.  
  163.     delete clipShape;
  164.     
  165.     DrawGrid(gc);
  166.     
  167.     gc->SetClipShape(svClipShape);
  168.  
  169.     // ----- Draw selections Handles -----
  170.     if (GetFrame()->IsActive())
  171.     {
  172.         ((CDrawSelection*)fDrawPart->GetSelection())->DrawSelectionHandles(this, TRUE);
  173.     }
  174.  
  175.     delete svClipShape;
  176.     
  177. }
  178.  
  179. //------------------------------------------------------------------------------
  180. // CDrawFacet::MouseEnterFrame
  181. //------------------------------------------------------------------------------
  182.  
  183. void CDrawFacet::MouseEnter(const FW_CPoint& /*where*/)
  184. {
  185.     if (fDrawPart->GetActiveFrame() != GetFrame() || fDrawPart->GetTool() == kSelectTool)
  186.         ::InitCursor();
  187.     else
  188.         ::SetCursor(*::GetCursor(crossCursor));
  189. }
  190.  
  191. //------------------------------------------------------------------------------
  192. // CDrawFacet::DoMouseDownInEmbeddedFrame
  193. //------------------------------------------------------------------------------
  194.  
  195. FW_Boolean CDrawFacet::DoMouseDownInEmbeddedFrame(XMPFacet* embeddedXMPFacet, 
  196.                                                 const FW_CPoint& where, 
  197.                                                 XMPEventData event)
  198. {
  199.     CDrawProxyRun *drawProxyRun = (CDrawProxyRun*)fDrawPart->XMPFrametoProxyRun(embeddedXMPFacet->GetFrame());
  200.     if (drawProxyRun->IsSelected())
  201.     {
  202.         if (!::WaitMouseMoved(event->where))
  203.         {
  204.             drawProxyRun->SetSelectState(FALSE);
  205.             event->what = mouseDown;
  206.             fDrawPart->GetSession()->GetDispatcher()->Dispatch(event);
  207.             if (GetFrame()->IsActive())
  208.             {
  209.                 drawProxyRun->SetSelectState(TRUE);
  210.             }
  211.             return TRUE;
  212.         }
  213.     }
  214.     
  215.     return DoMouseDown(where, event);
  216. }
  217.  
  218. //------------------------------------------------------------------------------
  219. // CDrawFacet::DoMouseDownInEmbeddedFrameBorder
  220. //------------------------------------------------------------------------------
  221.  
  222. FW_Boolean CDrawFacet::DoMouseDownInEmbeddedFrameBorder(XMPFacet* embeddedXMPFacet, 
  223.                                                         const FW_CPoint& where, 
  224.                                                         XMPEventData event)
  225. {
  226. FW_UNUSED(embeddedXMPFacet);
  227.  
  228.     FW_CGraphicContext gc(*this);
  229.  
  230.     fDrawPart->SetTool(kSelectTool);
  231.     
  232.     CDrawProxyRun *drawProxyRun = (CDrawProxyRun*)fDrawPart->XMPFrametoProxyRun(embeddedXMPFacet->GetFrame());
  233.     CProxyShape *theShape = drawProxyRun->GetShape();
  234.     CDrawSelection* selection = (CDrawSelection*)fDrawPart->GetSelection();
  235.  
  236.     if (::TestShiftKey(event))
  237.     {
  238.         if (theShape->IsSelected())
  239.         {
  240.             selection->RemoveFromSelection(theShape, TRUE);
  241.             return TRUE;
  242.         }
  243.         else
  244.             selection->AddToSelection(theShape, TRUE);
  245.         
  246.         // ----- Clear the shift key flag
  247.         event->modifiers -= 0x0200;
  248.     }
  249.     else if (!theShape->IsSelected())
  250.     {
  251.         selection->CloseSelection();
  252.         selection->AddToSelection(theShape, TRUE);
  253.     }
  254.  
  255.     // ----- if a shape is selected start dragging
  256.     if (theShape->IsSelected())
  257.     {
  258.         FW_SPlatformPoint anchorPoint = where;
  259.         short whichHandle = theShape->WhichHandle(this, anchorPoint);
  260.         selection->SetAnchorShape(theShape);
  261.         if (whichHandle != 0)
  262.             selection->Resize(&gc, whichHandle, anchorPoint, event);
  263.         else 
  264.             selection->Drag(this, event);
  265.     }
  266.  
  267.     return TRUE;    
  268. //    return DoMouseDown(where, event);
  269.  
  270. }
  271.  
  272. //------------------------------------------------------------------------------
  273. // CDrawFacet::DoMouseDown
  274. //------------------------------------------------------------------------------
  275.  
  276. FW_Boolean CDrawFacet::DoMouseDown(const FW_CPoint& where, XMPEventData event)
  277. {
  278.     FW_CGraphicContext gc(*this);
  279.  
  280.     FW_SPlatformPoint anchorPoint = gc.AsPlatformPoint(where);
  281.     
  282.     if (fDrawPart->GetTool() == kSelectTool)
  283.     {
  284.         CDrawSelection* selection = (CDrawSelection*)fDrawPart->GetSelection();
  285.  
  286.         // ----- Click in an handle
  287.         short whichHandle;
  288.         CBaseShape* shape = selection->WhichHandle(this, anchorPoint, whichHandle);
  289.         if (shape != NULL)
  290.         {
  291.             selection->SetAnchorShape(shape);
  292.             selection->Resize(&gc, whichHandle, anchorPoint, event);
  293.             fDrawPart->Changed();
  294.             return TRUE;
  295.         }
  296.         
  297.         // ----- if not in an handle, look for a shape
  298.         shape = WhichShape(&gc, anchorPoint, event);
  299.         
  300.         // ----- 
  301.         if (shape == NULL)
  302.         {
  303.             // ----- Draw a selection rectangle
  304.             selection->SelectWithRectangle(this, anchorPoint, event);
  305.         }
  306.         else
  307.         {
  308.             if (::TestShiftKey(event))
  309.             {
  310.                 if (shape->IsSelected())
  311.                     selection->RemoveFromSelection(shape, TRUE);
  312.                 else
  313.                     selection->AddToSelection(shape, TRUE);
  314.             }
  315.             else if (!shape->IsSelected())
  316.             {
  317.                 selection->CloseSelection();
  318.                 selection->AddToSelection(shape, TRUE);
  319.             }            
  320.             
  321.             // ----- if a shape is selected start dragging
  322.             if (shape->IsSelected())
  323.             {
  324.                 selection->SetAnchorShape(shape);
  325.                 selection->Drag(this, event);
  326.                 fDrawPart->Changed();
  327.             }
  328.         }
  329.             
  330.         return TRUE;
  331.     }
  332.     
  333.     CBaseShape *theShape =  fDrawPart->NewShape(fDrawPart->GetTool() - kLine + kLineShape);
  334.         
  335.     FW_CStyle trackStyle(fDrawPart->GetPenSize());
  336.  
  337.     if (theShape->Track(&gc, trackStyle, anchorPoint, event))
  338.     {
  339.         FW_CColor fillColor, penColor;
  340.         fDrawPart->GetFillColor(&fillColor);
  341.         fDrawPart->GetPenColor(&penColor);
  342.         
  343.         FW_CInk brushInk(fillColor);
  344.         FW_CStyle penStyle(fDrawPart->GetPenSize());
  345.         FW_CInk penInk(penColor);
  346.         
  347.         theShape->SetBrushInk(brushInk);    // We use the standard style for the brush
  348.         theShape->SetPenStyle(penStyle);
  349.         theShape->SetPenInk(penInk);
  350.  
  351.         theShape->SetFrameFillStyle(fDrawPart->GetFillFrame());
  352.                 
  353.         fDrawPart->AddShape(theShape);
  354.         fDrawPart->Changed();
  355.         
  356.         fDrawPart->ClipEmbeddedFrames(fDrawPart->GetMainPresentation());
  357.         theShape->DrawShape(&gc);
  358.         
  359.         XMPShape *invalidShape = ::NewXMPShape();
  360.         theShape->GetUpdateShape(invalidShape);
  361.         this->InvalidateAllFacets(invalidShape, TRUE);
  362.         delete invalidShape;
  363.     }
  364.     else
  365.     {
  366.         delete theShape;
  367.     }
  368.     
  369.     return TRUE;
  370. }
  371.  
  372. //------------------------------------------------------------------------------
  373. // CDrawFacet::WhichShape
  374. //------------------------------------------------------------------------------
  375.  
  376. CBaseShape* CDrawFacet::WhichShape(FW_CGraphicContext* gc, FW_SPlatformPoint mouse, XMPEventData event)
  377. {
  378.     BC_TCollectionActiveIterator<CBaseShape*> ite(*fDrawPart->GetShapeList());
  379.     CBaseShape* shape;
  380.     while (!ite.IsDone())
  381.     {
  382.         shape = *ite.CurrentItem();
  383.         if (shape->HitTest(gc, mouse, event))
  384.             return shape;    
  385.         ite.Next();
  386.     }
  387.     
  388.     return NULL;
  389. }
  390.  
  391. //------------------------------------------------------------------------------
  392. // CDrawFacet::DrawGrid
  393. //------------------------------------------------------------------------------
  394.  
  395. void CDrawFacet::DrawGrid(FW_CGraphicContext* gc)
  396. {
  397.     FW_CRect extend;
  398.     ((CDrawFrame*)GetFrame())->GetExtendRect(&extend);
  399.     
  400.     char vertical[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF};
  401.     char horizontal[8] = {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01};
  402.         
  403.     FW_CLineShape line(FW_CPoint(ff(-1), 0), FW_CPoint(ff(-1), extend.bottom - ff(1)));
  404.     line->SetPattern(vertical);
  405.     
  406.     for (XMPCoordinate x = ff(-1); x <= extend.right; x += ff(72))
  407.     {
  408.         line->Draw(gc);
  409.         line->MoveShape(ff(72), 0);
  410.     }
  411.     
  412.     line->SetPattern(horizontal);
  413.     line->SetLineStart(FW_CPoint(0, ff(-1)));
  414.     line->SetLineEnd(FW_CPoint(extend.right - ff(1), ff(-1)));
  415.     
  416.     for (XMPCoordinate y = ff(-1); y <= extend.bottom; y += ff(72))
  417.     {
  418.         line->Draw(gc);
  419.         line->MoveShape(0, ff(72));
  420.     }
  421. }
  422.  
  423. //------------------------------------------------------------------------------
  424. // CDrawFacet::ClipEmbeddedFacets
  425. //------------------------------------------------------------------------------
  426.  
  427. void CDrawFacet::ClipEmbeddedFacets()
  428. {
  429.     XMPFacet* xmpFacet = GetXMPFacet();
  430.     
  431.     XMPShape* clipShape = ::NewXMPShape();
  432.     XMPTransform* wclipTrans = ::NewXMPTransform();
  433.     
  434.     XMPShape* workingClip = ::NewXMPShape(GetClipShape());
  435.     workingClip->Transform(GetWindowFrameTransform());        // so now in window coordinate
  436.     
  437.     BC_TCollectionActiveIterator<CBaseShape*> ite(*fDrawPart->GetShapeList());
  438.     CBaseShape* shape;
  439.     while (!ite.IsDone())
  440.     {
  441.         shape = *ite.CurrentItem();
  442.         if (shape->GetShapeType() == kProxyShape)
  443.         {    
  444.             BC_TCollectionActiveIterator<FW_CProxyFrame*> ite2(*((CProxyShape*)shape)->GetProxyRun()->GetProxyFrameList());
  445.             FW_CProxyFrame* proxyFrame;
  446.             while (!ite2.IsDone())
  447.             {
  448.                 proxyFrame = *ite2.CurrentItem();
  449.                 if (proxyFrame->GetContainingFrame() == (FW_CEmbeddingFrame*)this->GetFrame())
  450.                 {
  451.                     XMPFrameFacetIterator ite3(proxyFrame->GetXMPFrame());
  452.                     for (XMPFacet *embeddedXMPFacet = ite3.First(); ite3.IsNotComplete(); embeddedXMPFacet = ite3.Next())
  453.                     {
  454.                         if (embeddedXMPFacet->GetContainingFacet() == xmpFacet)
  455.                         {
  456.                             XMPShape* embeddedClip = ::NewXMPShape(embeddedXMPFacet->GetFrame()->GetFrameShape());
  457.                             embeddedClip->Intersect(embeddedXMPFacet->GetFrame()->GetUsedShape());
  458.                                                         
  459.                             // ----- Transform workingClip into embeddedXMPFacet frame coordinate -----
  460.                             wclipTrans->CopyFrom(embeddedXMPFacet->GetWindowFrameTransform());
  461.                             wclipTrans->Invert();
  462.                             workingClip->Transform(wclipTrans);
  463.                             
  464.                             // ----- Set the clip of embeddedXMPFacet -----
  465.                             embeddedClip->Intersect(workingClip);
  466.                             workingClip->Subtract(embeddedClip);
  467.                             embeddedXMPFacet->ChangeGeometry(embeddedClip, NULL);
  468.                 
  469.                             // ----- put back workingClip to window coordinate
  470.                             wclipTrans->Invert();
  471.                             workingClip->Transform(wclipTrans);
  472.                         }
  473.                     }                    
  474.                 }
  475.                 ite2.Next();
  476.             }                        
  477.         }
  478.         else
  479.         {
  480.             // ----- remove the clipShape of the shape -----
  481.             FW_CGraphicContext gc(*this);
  482.             clipShape->CopyFrom(shape->GetClipShape(&gc));
  483.             clipShape->Transform(GetWindowContentTransform());    // in window coordinate
  484.             workingClip->Subtract(clipShape);
  485.         }
  486.         ite.Next();
  487.     }
  488.     
  489.     delete clipShape;
  490.     delete workingClip;
  491.     delete wclipTrans;
  492. }
  493.  
  494. //------------------------------------------------------------------------------
  495. // CDrawFacet::CreateEmbeddedFacet
  496. //------------------------------------------------------------------------------
  497. //    We need to set the selected flag in the newly created embedded facet
  498.  
  499. XMPFacet* CDrawFacet::CreateEmbeddedFacet(FW_CProxyFrame *itsFrame, XMPShape* clipShape, XMPTransform* externalTransform, 
  500.                                             XMPFacet* siblingFacet, XMPFramePosition position)
  501. {
  502.     XMPFacet* embeddedXMPFacet = FW_CFacet::CreateEmbeddedFacet(itsFrame, 
  503.                                                             clipShape, 
  504.                                                             externalTransform,
  505.                                                             siblingFacet, 
  506.                                                             position);
  507.     
  508.     embeddedXMPFacet->SetSelected(itsFrame->GetProxyRun()->IsSelected());
  509.     
  510.     return embeddedXMPFacet;
  511. }
  512.  
  513.